package cn.waqwb.bwcar.service.impl;

import cn.waqwb.bwcar.dao.SysMenuMapper;
import cn.waqwb.bwcar.dto.DataGridResult;
import cn.waqwb.bwcar.dto.QueryDTO;
import cn.waqwb.bwcar.pojo.SysMenu;
import cn.waqwb.bwcar.service.MenuService;
import cn.waqwb.bwcar.utils.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author JokerQ
 * @date 2019/11/7 - 10:44
 */
@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Override
    public DataGridResult findMenu(QueryDTO queryDTO) {
        PageHelper.offsetPage(queryDTO.getOffset(), queryDTO.getLimit());
        if (queryDTO.getSort() != null && !queryDTO.getSort().equals("")) {
            queryDTO.setSort("menu_id");
        }
        List<SysMenu> menuByPage = sysMenuMapper.findMenuByPage(queryDTO);
        PageInfo<SysMenu> info = new PageInfo<SysMenu>(menuByPage);
        long total = info.getTotal();
        DataGridResult result = new DataGridResult(total, info.getList());
        return result;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)//事务注解
    public R deleteMenu(List<Long> ids) {
        for (Long id : ids) {
            if (id < 51) {
                return R.error("系统菜单，不可删除");
            }
        }

        int i = sysMenuMapper.deleteMenu(ids);
        if (i > 0) {
            return R.ok();
        } else {
            return R.error(-200, "删除失败");
        }
    }

    @Override
    public R selectMenu() {
        List<SysMenu> menus = sysMenuMapper.findMenu();
        //添加根目录
        SysMenu sysMenu = new SysMenu();
        sysMenu.setMenuId(0L);
        sysMenu.setType(0);
        sysMenu.setParentId(-1L);
        sysMenu.setName("一级菜单");
        menus.add(sysMenu);
        return R.ok().put("menuList", menus);
    }

    @Override
    public R saveMenu(SysMenu sysMenu) {
        int i = sysMenuMapper.insertSelective(sysMenu);

        return i > 0 ? R.ok() : R.error("新增失败");
    }

    @Override
    public R findMenuById(Long menuId) {
        SysMenu sysMenu = sysMenuMapper.selectByPrimaryKey(menuId);
        return R.ok().put("menu", sysMenu);
    }

    @Override
    public R updateMenu(SysMenu sysMenu) {
        int i = sysMenuMapper.updateByPrimaryKeySelective(sysMenu);
        return i > 0 ? R.ok() : R.error("修改失败");
    }

    @Override
    public List<String> findPermsByUserId(Long userId) {
        List<String> permsByUserId = sysMenuMapper.findPermsByUserId(userId);
        Set<String> set = new HashSet<String>();
        for (String string : permsByUserId) {
            if (string != null && !string.equals("")) {
                String[] strings = string.split(",");
                for (String str : strings) {
                    set.add(str);
                }
            }
        }
        List<String> perms = new ArrayList<String>();
        perms.addAll(set);
        return perms;
    }

    @Override
    public R findUserMenu(Long userId) {
        //查询用户的一级目录
        List<Map<String,Object>> dirMenuByUserId = sysMenuMapper.findDirMenuByUserId(userId);
        //查询目录对应的子菜单
        for (Map<String ,Object> map: dirMenuByUserId) {
            Long menuId = Long.parseLong(map.get("menuId")+"");
            List<Map<String ,Object>> subList = sysMenuMapper.findMenuNotButtonByUserId(userId,menuId);
            map.put("list",subList);
        }
        List<String> permsByUserId = sysMenuMapper.findPermsByUserId(userId);
        return R.ok().put("menuList",dirMenuByUserId).put("permissions",permsByUserId);
    }


}
